package com.codefish.framework.spring.context.support;

import com.codefish.framework.spring.beans.exception.BeansException;
import com.codefish.framework.spring.beans.factory.config.IBeanPostProcessor;
import com.codefish.framework.spring.context.IApplicationContext;
import com.codefish.framework.spring.context.IApplicationContextAware;

/**
 *  在执行bean的初始化前置方法阶段执行实现了{@link IApplicationContextAware}接口的bean的回调方法。
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/10/08 上午 11:13
 */
public class ApplicationAwarePostProcessor implements IBeanPostProcessor {
    private final IApplicationContext applicationContext;

    public ApplicationAwarePostProcessor(IApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof IApplicationContextAware) {
            //执行ApplicationContextAware的回调方法
            ((IApplicationContextAware) bean).setApplicationContext(this.applicationContext);
        }
        return bean;
    }
}
